package com.dnjn.mes.biz.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dnjn.mes.biz.domain.MesRecordCapacity;
import com.dnjn.mes.biz.domain.MesWorkforceShift;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalTime;
import java.util.List;

/**
 * <p>
 * 产能表 Mapper 接口
 * </p>
 *
 * @author Amber
 * @since 2023-07-26
 */
public interface MesRecordCapacityMapper extends BaseMapper<MesRecordCapacity> {

    //早班 7号早上八点-晚八点
//    @Select("select * from mes_record_capacity where TO_DAYS(create_time) = TO_DAYS(CURDATE()) and DATE_FORMAT(create_time,'%H:%i:%s') BETWEEN  '08:00:00' and '20:00:00' order by create_time ")
    @Select("select * from mes_record_capacity where TO_DAYS(`cur_date`) = TO_DAYS(CURDATE()) and equipment_id=#{equipmentId} and current_hour BETWEEN 8 and 20 ORDER BY create_time")
    List<MesRecordCapacity> getEarlyRecordCapacityChart(@Param("equipmentId") long equipmentId);

    //晚班：7号晚八点 - 8号早八点
    //@Select("select * from mes_record_capacity where TO_DAYS(create_time) = TO_DAYS(CURDATE()) and DATE_FORMAT(create_time,'%H:%i:%s') BETWEEN  '00:00:00' and '08:00:00' or ( TO_DAYS(create_time) - TO_DAYS(CURDATE()) <= 1 and DATE_FORMAT(create_time,'%H:%i:%s') BETWEEN  '20:00:00' and '24:00:00') ORDER BY create_time ")
    @Select("select * from mes_record_capacity where TO_DAYS(`cur_date`) = TO_DAYS(CURDATE()) and current_hour IN (21,22,23) AND equipment_id=#{equipmentId} or ( TO_DAYS(`cur_date`) - TO_DAYS(CURDATE()) = 1 and equipment_id=#{equipmentId} and current_hour BETWEEN 0 and 8) ORDER BY create_time ")
    List<MesRecordCapacity> getNightRecordCapacityChart(@Param("equipmentId") long equipmentId);

    @Select("SELECT capacity FROM mes_record_capacity where `cur_date` = DATE(NOW()) and current_hour = #{hour} and equipment_id=#{equipmentId} limit 1")
    Integer getLastHourData(@Param("hour") int hour,@Param("equipmentId") long equipmentId);
    @Select("select SUM(capacity) from mes_record_capacity where TO_DAYS(`cur_date`) = TO_DAYS(CURDATE()) and (current_hour BETWEEN 8 and 23 )  AND equipment_id=#{equipmentId} or (TO_DAYS(`cur_date`) - TO_DAYS(CURDATE()) = 1 and current_hour BETWEEN 0 and 8) AND equipment_id=#{equipmentId} ")
    int sumTodayRecordCapacity(@Param("equipmentId") long equipmentId);

//    @Select(" SELECT cur_date,current_hour,capacity,equipment_id,create_time FROM mes_record_capacity WHERE 1=1 and ( cur_date >= #{startLocalDateTime}  and  current_hour >=#{startTime}) and ( cur_date >= #{endLocalDateTime}  and  current_hour < #{endTime} )AND equipment_id = #{equipmentId} ORDER BY create_time")
    @Select(" SELECT * FROM mes_record_capacity c where 1=1 and c.current_date_hour >=#{startLocalDateTime} and c.current_date_hour < #{endLocalDateTime} and c.equipment_id=#{equipmentId} ")
    List<MesRecordCapacity> getEarlyTrendsCapacityByTime(@Param("equipmentId") long equipmentId, @Param("startTime")LocalTime startTime, @Param("endTime")LocalTime endTime,@Param("startLocalDateTime") String startLocalDateTime,@Param("endLocalDateTime") String endLocalDateTime);

    //产量统计->统计每天总产量
//    @Select(" SELECT cur_date,TRUNCATE(sum(capacity)/(SELECT SUM(capacity) FROM mes_record_capacity WHERE equipment_id=#{equipmentId} AND current_date_hour BETWEEN #{startTime} AND #{endTime} ),2) AS ratio FROM mes_record_capacity WHERE equipment_id=#{equipmentId} AND cur_date BETWEEN #{startTime} AND #{endTime}  GROUP BY cur_date ORDER BY cur_date")
    @Select(" SELECT cur_date,sum(capacity) AS ratio FROM mes_record_capacity WHERE equipment_id=#{equipmentId} AND cur_date BETWEEN #{startTime} AND #{endTime}  GROUP BY cur_date ORDER BY cur_date")
    List<MesRecordCapacity> countRecordCapacitys(@Param("startTime") String startTime, @Param("endTime")String endTime,@Param("equipmentId") long equipmentId);

}
